package cn.jeeweb.web.modules.sys.controller;

import cn.jeeweb.common.http.Response;
import cn.jeeweb.common.mvc.annotation.ViewPrefix;
import cn.jeeweb.common.mybatis.mvc.wrapper.EntityWrapper;
import cn.jeeweb.common.query.data.PropertyPreFilterable;
import cn.jeeweb.common.utils.FastJsonUtils;
import cn.jeeweb.web.aspectj.annotation.Log;
import cn.jeeweb.web.modules.sys.entity.CodeTable;
import cn.jeeweb.web.modules.sys.service.ICodeTableService;
import cn.jeeweb.web.utils.PageRequest;
import cn.xjzn.framework_Core.ModuleActionType;
import com.alibaba.fastjson.serializer.SerializeFilter;
import com.baomidou.mybatisplus.plugins.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;


/**
 *
 */
@RestController
@RequestMapping("/sys/codetable")
@ViewPrefix("modules/codetable")
//@RequiresPathPermission("ui:codetable")
@Log(title = "code_table")
public class CodeTableController{
	@Autowired
	private ICodeTableService codeTableService;

	@PostMapping("add")
	@Log(logType = ModuleActionType.INSERT)
//	@RequiresMethodPermissions("add")
	public Response add(CodeTable entity, BindingResult result,
						HttpServletRequest request, HttpServletResponse response) {
		// 验证错误
		codeTableService.insert(entity);
		return Response.ok("添加成功");
	}

	@PostMapping("{id}/update")
	@Log(logType = ModuleActionType.UPDATE)
//	@RequiresMethodPermissions("update")
	public Response update(CodeTable entity, BindingResult result,
						   HttpServletRequest request, HttpServletResponse response) {
		// 验证错误
		codeTableService.insertOrUpdate(entity);
		return Response.ok("更新成功");
	}

	@PostMapping("{id}/delete")
	@Log(logType = ModuleActionType.DELETE)
//	@RequiresMethodPermissions("delete")
	public Response delete(@PathVariable("id") String id) {
		codeTableService.deleteById(id);
		return Response.ok("删除成功");
	}

	/**
	 *
	 * @throws IOException
     */
	@RequestMapping(value = "getList", method = { RequestMethod.GET, RequestMethod.POST })
	@Log(logType = ModuleActionType.SELECT)
//	@RequiresMethodPermissions("list")
	public void ajaxList(PropertyPreFilterable propertyPreFilterable, HttpServletRequest request, HttpServletResponse response) throws IOException {
		EntityWrapper<CodeTable> entityWrapper = new EntityWrapper<>(CodeTable.class);
		propertyPreFilterable.addQueryProperty(new String[]{"id","name","code","prefix","dateFormat","maxNumber"});
		// 预处理
		SerializeFilter filter = propertyPreFilterable.constructFilter(CodeTable.class);
		// 预处理
		Page pageBean = codeTableService.selectPage(PageRequest.getPage(),entityWrapper);
		FastJsonUtils.print(pageBean);
	}

	@PostMapping("getCode")
	@Log(logType = ModuleActionType.SELECT)
	public String getCode(CodeTable codeTable){
		String code = codeTableService.createNewCode(codeTable);
		return code;
	}

}